/*
 * @Author: 来自火星的码农 15122322+heyzhi@user.noreply.gitee.com
 * @Date: 2025-10-01 17:55:07
 * @LastEditors: 来自火星的码农 15122322+heyzhi@user.noreply.gitee.com
 * @LastEditTime: 2025-10-01 18:02:56
 * @FilePath: /GateServer/tests/test_log.cc
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AEnt 
 */
#include "../src/util/logger.h"
#include <iostream>
void test_basic_logging() {
    std::cout << "=== Testing Basic Logging ===" << std::endl;
    
    // 测试C++风格接口
    Log::GetInstance().trace("This is a trace message: %d", 42);
    Log::GetInstance().info("This is an info message: %s", "Hello");
    Log::GetInstance().error("This is an error message: %f", 3.14);
    
    // 测试宏接口
    LOG_TRACE("Macro trace: {}", 123);
    LOG_INFO("Macro info: {}", "World");
    LOG_ERROR("Macro error: {}", 404);
    
    // 测试C风格接口
    LogTrace("C-style trace: %d", 100);
    LogDebug("C-style debug: %s", "debug message");
    LogInfo("C-style info: %d + %d = %d", 2, 3, 5);
    LogWarn("C-style warning: %x", 255);
    LogError("C-style error: %s", "something went wrong");
    LogCritical("C-style critical: %f", 99.99);
}

void test_log_levels() {
    std::cout << "\n=== Testing Log Levels ===" << std::endl;
    
    SetLogTrace();  // 设置最低级别，所有日志都会输出
    LogTrace("This should appear - TRACE level");
    LogDebug("This should appear - DEBUG level");
    LogInfo("This should appear - INFO level");
    
    SetLogInfo();   // 设置INFO级别，只有INFO及以上会输出
    LogTrace("This should NOT appear - TRACE level");
    LogDebug("This should NOT appear - DEBUG level");
    LogInfo("This should appear - INFO level");
    LogError("This should appear - ERROR level");
    
    SetLogOff();    // 关闭所有日志
    LogError("This should NOT appear - ERROR level (logging off)");
    
    SetLogTrace();  // 恢复TRACE级别
}

void test_multithreaded_logging() {
    std::cout << "\n=== Testing Multithreaded Logging ===" << std::endl;
    
    const int num_threads = 5;
    const int logs_per_thread = 10;
    
    std::vector<std::thread> threads;
    
    for (int i = 0; i < num_threads; ++i) {
        threads.emplace_back([i]() {
            for (int j = 0; j < logs_per_thread; ++j) {
                LogInfo("Thread %d - Message %d", i, j);
            }
        });
    }
    
    for (auto& t : threads) {
        t.join();
    }
    
    std::cout << "Multithreaded logging test completed" << std::endl;
}

void test_exception_safety() {
    std::cout << "\n=== Testing Exception Safety ===" << std::endl;
    
    try {
        // 测试无效路径（可能需要权限）
        auto& logger = Log::GetInstance("/invalid/path/that/probably/doesnt/exist/");
        std::cout << "Unexpected: Should have thrown exception" << std::endl;
    } catch (const std::exception& e) {
        std::cout << "Expected exception: " << e.what() << std::endl;
    }
    
    // 恢复使用正常路径
    Log::GetInstance("./logs/").info("Back to normal logging");
}

void test_log_rotation() {
    std::cout << "\n=== Testing Log Rotation ===" << std::endl;
    
    // 生成大量日志来测试文件滚动
    for (int i = 0; i < 1000; ++i) {
        LogInfo("Rotation test message %d - %s", i, 
                "This is a test message to fill up the log file quickly");
    }
    
    std::cout << "Log rotation test completed" << std::endl;
}

int main() {
    std::cout << "Starting Logger Test Suite..." << std::endl;
    
    try {
        test_basic_logging();
        test_log_levels();
        test_multithreaded_logging();
        test_exception_safety();
        test_log_rotation();
        
        std::cout << "\n=== All Tests Completed Successfully ===" << std::endl;
        std::cout << "Check the ./logs/ directory for generated log files" << std::endl;
        
    } catch (const std::exception& e) {
        std::cerr << "Test failed with exception: " << e.what() << std::endl;
        return 1;
    }
    
    return 0;
}